package com.example.smartcity_40.lawyer;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.SearchView;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.viewpager2.widget.ViewPager2;

import com.example.smartcity_40.R;
import com.example.smartcity_40.lawyer.apter.LawyerBannerApter;
import com.example.smartcity_40.lawyer.apter.PreferredlawyerApter;
import com.example.smartcity_40.lawyer.bean.LawyerBannerBean;
import com.example.smartcity_40.lawyer.bean.LegalexpertiseBean;
import com.example.smartcity_40.lawyer.bean.PreferredlawyerBean;
import com.example.smartcity_40.me.LoginActivity;
import com.example.smartcity_40.tool.Tool;

import java.util.ArrayList;

import kotlin.Unit;
import kotlin.jvm.functions.Function0;
import kotlin.jvm.functions.Function1;


public class LawyerActivity extends AppCompatActivity {

    private LawyerActivity context;
    private RecyclerView preferred_lawyer;
    private RecyclerView legal_expertise;
    private ViewPager2 banner;
    private SearchView sear;
    private Tool tool;
    private LawyerBannerApter lawyerBannerApter;
    private PreferredlawyerApter preferredlawyerApter;
    private TextView lawyerseek;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lawyer);
        context = this;
        tool = new Tool(context);
       /* if(TextUtils.isEmpty(tool.get("token"))){
            tool.snackBran(findViewById(R.id.back), "未登录", "登录", new Function0<Unit>() {
                @Override
                public Unit invoke() {
                    Intent intent = new Intent(context, LoginActivity.class);
                    context.startActivity(intent);
                    return null;
                }
            });
        }*/
        initview();
        bannermethod();
        legalexpertisemethod();
        preferredlawyer();
    }

    private void preferredlawyer() {
        tool.send("/prod-api/api/lawyer-consultation/lawyer/list-top10",
                Tool.GET,
                null,
                true,
                PreferredlawyerBean.class,
                new Function1<PreferredlawyerBean, Unit>() {
                    @Override
                    public Unit invoke(PreferredlawyerBean preferredlawyerBean) {
                        if (preferredlawyerBean.getCode() == 200) {
                            preferred_lawyer.setLayoutManager(new LinearLayoutManager(context));
                            preferredlawyerApter.setDate(preferredlawyerBean.getData());
                            preferred_lawyer.setAdapter(preferredlawyerApter);
                        }
                        return null;
                    }
                });
    }

    private void legalexpertisemethod() {
        tool.send("/prod-api/api/lawyer-consultation/legal-expertise/list",
                Tool.GET,
                null,
                true,
                LegalexpertiseBean.class,
                new Function1<LegalexpertiseBean, Unit>() {
                    @Override
                    public Unit invoke(LegalexpertiseBean legalexpertiseBean) {
                        if (legalexpertiseBean.getCode() == 200) {
                            legal_expertise.setLayoutManager(new GridLayoutManager(context,2,RecyclerView.HORIZONTAL,false));
                            lawyerBannerApter.setDate(legalexpertiseBean.getRows());
                            legal_expertise.setAdapter(lawyerBannerApter);
                        }
                        return null;
                    }
                });
    }

    private void bannermethod() {
        tool.send("/prod-api/api/lawyer-consultation/ad-banner/list",
                Tool.GET,
                null,
                true,
                LawyerBannerBean.class,
                new Function1<LawyerBannerBean, Unit>() {
                    @Override
                    public Unit invoke(LawyerBannerBean lawyerBannerBean) {
                        if (lawyerBannerBean.getCode() == 200) {
                            ArrayList<String> strings = new ArrayList<>();
                            for (LawyerBannerBean.DataBean datum : lawyerBannerBean.getData()) {
                                strings.add(datum.getImgUrl());
                            }
                            tool.setBanner(banner,strings);
                        }
                        return null;
                    }
                });
    }

    private void initview() {

        ImageView back = findViewById(R.id.back);
        sear = findViewById(R.id.sear);
        banner = findViewById(R.id.banner);
        legal_expertise = findViewById(R.id.legal_expertise);
        preferred_lawyer = findViewById(R.id.preferred_lawyer);
        lawyerseek = findViewById(R.id.lawyerseek);


        lawyerBannerApter = new LawyerBannerApter(context);
        preferredlawyerApter = new PreferredlawyerApter(context);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                context.finish();
            }
        });

        lawyerseek.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                context.startActivity(new Intent(context,LawyerSeekActivity.class));
            }
        });
    }
}